/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package gui;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import main.Bot;
import main.Main;
import main.SettingsHandler;

import org.apache.log4j.Logger;

import tools.BCrypt;
import database.ConnectionHandler;
import database.DBConnectionFactory;

/**
 * The main GUI class. Created mostly by visual editor, so it's messy
 * 
 * 
 */
public class GUI {

   class InstallDocumentListener implements DocumentListener {
	  public void changedUpdate(final DocumentEvent e) {
		 if (commandPrefix.getText().length() == 1 && !ircServer.getText().equals("") && !botNick.getText().equals("")
			   && kdChannel.getText().matches("[#]{1}.*")
			   && ageStart.getText().matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")
			   && kdLoc.getText().matches("[0-9]{1,2}:[0-9]{1,2}") && !botPassword.getText().equals("")) {
			saveBaseSettings.setEnabled(true);
		 } else {
			saveBaseSettings.setEnabled(false);
		 }
	  }

	  public void insertUpdate(final DocumentEvent e) {
		 if (commandPrefix.getText().length() == 1 && !ircServer.getText().equals("") && !botNick.getText().equals("")
			   && kdChannel.getText().matches("[#]{1}.*")
			   && ageStart.getText().matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")
			   && kdLoc.getText().matches("[0-9]{1,2}:[0-9]{1,2}") && !botPassword.getText().equals("")) {
			saveBaseSettings.setEnabled(true);
		 } else {
			saveBaseSettings.setEnabled(false);
		 }
	  }

	  public void removeUpdate(final DocumentEvent e) {
		 if (commandPrefix.getText().length() == 1 && !ircServer.getText().equals("") && !botNick.getText().equals("")
			   && kdChannel.getText().matches("[#]{1}.*")
			   && ageStart.getText().matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")
			   && kdLoc.getText().matches("[0-9]{1,2}:[0-9]{1,2}") && !botPassword.getText().equals("")) {
			saveBaseSettings.setEnabled(true);
		 } else {
			saveBaseSettings.setEnabled(false);
		 }
	  }
   }

   private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="98,24"
   private JPanel jContentPane = null;
   private JToolBar jJToolBarBar1 = null;
   private JToggleButton connectButton = null;
   private JButton exitButton = null;
   private JPanel basePanel = null;
   private JButton installButton = null;
   private JLabel jLabel = null;
   private JButton newAgeButton = null;
   private JLabel jLabel1 = null;
   private JButton goToBotButton = null;
   private JLabel jLabel2 = null;
   private JDialog installDialog = null; // @jve:decl-index=0:visual-constraint="628,137"
   private JPanel installContentPane = null;
   private JLabel jLabel3 = null;
   private JTextField dbName = null;
   private JLabel jLabel4 = null;
   private JTextField dbUser = null;
   private JLabel jLabel5 = null;
   private JTextField dbPassword = null;
   private JButton testDBCredentials = null;
   private JButton createDB = null;
   private JLabel credentialsTestLabel = null;
   private JDialog baseSettingsDialog = null; // @jve:decl-index=0:visual-constraint="149,551"
   private JPanel baseSettingsContentPane = null;
   private JLabel jLabel6 = null;
   private JLabel jLabel7 = null;
   private JTextField ircServer = null;
   private JLabel jLabel8 = null;
   private JLabel jLabel9 = null;
   private JTextField botNick = null;
   private JLabel jLabel10 = null;
   private JTextField botPassword = null;
   private JLabel jLabel36 = null;
   private JTextField kdChannel = null;
   private JLabel jLabel37 = null;
   private JTextField adminChannel = null;
   private JLabel jLabel38 = null;
   private JCheckBox usersMayRegister = null;
   private JLabel jLabel39 = null;
   private JLabel jLabel41 = null;
   private JTextField ageStart = null;
   private JLabel jLabel43 = null;
   private JTextField kdLoc = null;
   private JButton saveBaseSettings = null;
   private JButton cancelBaseSettings = null;
   private JLabel jLabel45 = null;
   private JTextField superAdmin = null;
   private JLabel jLabel46 = null;
   private JButton addSuperAdmin = null;
   private JLabel jLabel47 = null;
   private JButton goToBaseSettingsDialog = null;
   private JButton cancelInstallDialog = null;
   private JLabel jLabel48 = null;
   private JLabel jLabel49 = null;
   private JLabel jLabel50 = null;
   private JLabel jLabel666 = null;
   private JCheckBox adminLimit = null;
   private JPanel botMainPanel = null;
   private JButton baseSettings = null;
   private JButton services = null;
   private JButton google = null;
   private JButton editSciences = null;
   private JButton editUsers = null;
   private JButton editLinks = null;
   private JButton editCommands = null;
   private JButton editBuildings = null;
   private JButton editRaces = null;
   private JButton editPersonalities = null;
   private JButton editTitles = null;
   private JButton editHelp = null;
   private JButton editTextInput = null;
   private JButton editWorkers = null;

   private JDialog settingsDialog = null; // @jve:decl-index=0:visual-constraint="635,637"

   private JCheckBox passwordAuth = null;

   private JButton editSMSSettings = null;

   private JButton toTrayButton;

   private JLabel jLabel11 = null;
   private JTextField dbHost = null;

   private JTextField commandPrefix = null;

   public GUI() {
	  getJFrame();
	  if (Main.getDbUpdated() > 0) {
		 showErrorMessage(Main.getDbUpdated() == 1 ? "Update to new bot version failed"
			   : "The bot has been updated to a new version. Please restart it", jContentPane);
		 System.exit(0);
	  }
	  getInstallDialog();
	  getBaseSettingsDialog();
	  getSettingsDialog();
	  createTrayIcon();
   }

   /**
    * Creates a tray icon for the bot
    */
   private void createTrayIcon() {
	  final TrayIcon trayIcon;
	  if (SystemTray.isSupported()) {
		 final SystemTray tray = SystemTray.getSystemTray();
		 Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/java.png"));
		 PopupMenu popup = new PopupMenu();
		 trayIcon = new TrayIcon(image, "Lucid Bot", popup);
		 trayIcon.setImageAutoSize(true);
		 MouseListener mouseListener = new MouseListener() {
			public void mouseClicked(final MouseEvent arg0) {
			   jFrame.setVisible(true);
			}

			public void mouseEntered(final MouseEvent arg0) {
			}

			public void mouseExited(final MouseEvent arg0) {
			}

			public void mousePressed(final MouseEvent arg0) {
			}

			public void mouseReleased(final MouseEvent arg0) {
			}
		 };
		 ActionListener openListener = new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
			   jFrame.setVisible(true);
			}
		 };
		 ActionListener exitListener = new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
			   System.exit(0);
			}
		 };
		 MenuItem openItem = new MenuItem("Open");
		 openItem.addActionListener(openListener);
		 MenuItem exitItem = new MenuItem("Exit");
		 exitItem.addActionListener(exitListener);
		 popup.add(openItem);
		 popup.add(exitItem);
		 trayIcon.addMouseListener(mouseListener);
		 try {
			tray.add(trayIcon);
		 } catch (AWTException e) {
			Logger.getLogger("Error").error("", e);
		 }
	  } else {
		 jFrame.setVisible(true);
	  }
   }

   /**
    * This method initializes addSuperAdmin
    * 
    * @return javax.swing.JButton
    */
   private JButton getAddSuperAdmin() {
	  if (addSuperAdmin == null) {
		 addSuperAdmin = new JButton();
		 addSuperAdmin.setText("Add Admin");
		 addSuperAdmin.setEnabled(false);
		 addSuperAdmin.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   final Connection con = DBConnectionFactory.getConnectionHandler().getConnection();
			   PreparedStatement ps = null;
			   try {
				  ps = con.prepareStatement("INSERT INTO kdmember" + " VALUES(0,0,'',?,'','')");
				  ps.setString(1, superAdmin.getText());
				  ps.executeUpdate();

				  try {
					 ps.close();
				  } catch (final Exception e2) {
				  }

				  ps = con.prepareStatement("INSERT INTO nickname" + " VALUES(?,?)");
				  ps.setString(1, superAdmin.getText());
				  ps.setString(2, superAdmin.getText());
				  ps.executeUpdate();

				  try {
					 ps.close();
				  } catch (final Exception e2) {
				  }

				  ps = con.prepareStatement("INSERT INTO admins" + " VALUES(?,true)");
				  ps.setString(1, superAdmin.getText());
				  ps.executeUpdate();

				  try {
					 ps.close();
				  } catch (final Exception e2) {
				  }

				  ps = con.prepareStatement("INSERT INTO botaccount" + " VALUES(?,'"
						+ BCrypt.hashpw("password", BCrypt.gensalt()) + "',true)");
				  ps.setString(1, superAdmin.getText());
				  ps.executeUpdate();

				  addSuperAdmin.setEnabled(false);
				  goToBaseSettingsDialog.setEnabled(true);
			   } catch (final SQLException error) {
				  showErrorMessage("Failed to add super admin", installContentPane);
			   } finally {
				  try {
					 ps.close();
				  } catch (final Exception e2) {
				  }
			   }
			}
		 });
	  }
	  return addSuperAdmin;
   }

   /**
    * This method initializes adminChannel
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getAdminChannel() {
	  if (adminChannel == null) {
		 adminChannel = new JTextField(25);
		 adminChannel.getDocument().addDocumentListener(new InstallDocumentListener());
	  }
	  return adminChannel;
   }

   /**
    * This method initializes adminLimit
    * 
    * @return javax.swing.JCheckBox
    */
   private JCheckBox getAdminLimit() {
	  if (adminLimit == null) {
		 adminLimit = new JCheckBox();
		 adminLimit.setText("Select for yes");
		 adminLimit.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(final javax.swing.event.ChangeEvent e) {
			   if (!ircServer.getText().equals("") && !botNick.getText().equals("") && kdChannel.getText().matches("[#]{1}.*")
					 && ageStart.getText().matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")
					 && kdLoc.getText().matches("[0-9]{1,2}:[0-9]{1,2}") && !botPassword.getText().equals("")) {
				  saveBaseSettings.setEnabled(true);
			   } else {
				  saveBaseSettings.setEnabled(false);
			   }
			}
		 });
	  }
	  return adminLimit;
   }

   /**
    * This method initializes ageStart
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getAgeStart() {
	  if (ageStart == null) {
		 ageStart = new JTextField(25);
		 ageStart.getDocument().addDocumentListener(new InstallDocumentListener());
	  }
	  return ageStart;
   }

   /**
    * This method initializes basePanel
    * 
    * @return javax.swing.JPanel
    */
   private JPanel getBasePanel() {
	  if (basePanel == null) {
		 final GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
		 gridBagConstraints5.insets = new Insets(0, 0, 2, 1);
		 gridBagConstraints5.gridy = 2;
		 gridBagConstraints5.ipadx = 30;
		 gridBagConstraints5.ipady = 50;
		 gridBagConstraints5.gridx = 1;
		 final GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		 gridBagConstraints4.insets = new Insets(0, 0, 2, 0);
		 gridBagConstraints4.gridy = 2;
		 gridBagConstraints4.ipadx = 40;
		 gridBagConstraints4.ipady = 15;
		 gridBagConstraints4.gridx = 0;
		 final GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		 gridBagConstraints3.insets = new Insets(0, 0, 0, 1);
		 gridBagConstraints3.gridy = 1;
		 gridBagConstraints3.ipadx = 30;
		 gridBagConstraints3.ipady = 50;
		 gridBagConstraints3.gridx = 1;
		 final GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		 gridBagConstraints2.gridx = 0;
		 gridBagConstraints2.ipadx = 40;
		 gridBagConstraints2.ipady = 15;
		 gridBagConstraints2.gridy = 1;
		 final GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		 gridBagConstraints1.insets = new Insets(0, 0, 0, 1);
		 gridBagConstraints1.gridy = 0;
		 gridBagConstraints1.ipadx = 40;
		 gridBagConstraints1.ipady = 50;
		 gridBagConstraints1.gridx = 1;
		 final GridBagConstraints gridBagConstraints = new GridBagConstraints();
		 gridBagConstraints.gridx = 0;
		 gridBagConstraints.ipadx = 30;
		 gridBagConstraints.ipady = 15;
		 gridBagConstraints.gridy = 0;
		 jLabel2 = new JLabel();
		 jLabel2.setText("Run bot with saved settings");
		 jLabel1 = new JLabel();
		 jLabel1.setText("Purges the database of old intel etc.");
		 jLabel = new JLabel();
		 jLabel.setText("Creates a database and lets you set the basic settings");
		 basePanel = new JPanel();
		 basePanel.setLayout(new GridBagLayout());
		 basePanel.add(getInstallButton(), gridBagConstraints);
		 basePanel.add(jLabel, gridBagConstraints1);
		 basePanel.add(getNewAgeButton(), gridBagConstraints2);
		 basePanel.add(jLabel1, gridBagConstraints3);
		 basePanel.add(getGoToBotButton(), gridBagConstraints4);
		 basePanel.add(jLabel2, gridBagConstraints5);
	  }
	  return basePanel;
   }

   private JButton getBaseSettings() {
	  if (baseSettings == null) {
		 baseSettings = new JButton();
		 baseSettings.setText("Edit Base Settings");
		 baseSettings.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   setBaseSettingsToDialog();
			   baseSettingsDialog.setVisible(true);
			}
		 });
	  }
	  return baseSettings;
   }

   /**
    * This method initializes baseSettingsContentPane
    * 
    * @return javax.swing.JPanel
    */
   private JPanel getBaseSettingsContentPane() {
	  if (baseSettingsContentPane == null) {
		 GridBagConstraints gridBagConstraints30 = new GridBagConstraints();
		 gridBagConstraints30.gridx = 1;
		 gridBagConstraints30.gridy = 9;
		 gridBagConstraints30.ipadx = 120;
		 gridBagConstraints30.ipady = 17;
		 final GridBagConstraints gridBagConstraints48 = new GridBagConstraints();
		 gridBagConstraints48.gridx = 1;
		 gridBagConstraints48.gridy = 8;
		 gridBagConstraints48.ipadx = 120;
		 gridBagConstraints48.ipady = 17;
		 final GridBagConstraints gridBagConstraints47 = new GridBagConstraints();
		 gridBagConstraints47.gridx = 1;
		 gridBagConstraints47.gridy = 13;
		 jLabel49 = new JLabel();
		 jLabel49.setText(" ");
		 final GridBagConstraints gridBagConstraints39 = new GridBagConstraints();
		 gridBagConstraints39.gridx = 0;
		 gridBagConstraints39.ipadx = 36;
		 gridBagConstraints39.ipady = 25;
		 gridBagConstraints39.gridy = 8;
		 final GridBagConstraints gridBagConstraints38 = new GridBagConstraints();
		 gridBagConstraints38.insets = new Insets(0, 0, 6, 1);
		 gridBagConstraints38.gridy = 14;
		 gridBagConstraints38.ipadx = 50;
		 gridBagConstraints38.ipady = 5;
		 gridBagConstraints38.gridx = 1;
		 final GridBagConstraints gridBagConstraints37 = new GridBagConstraints();
		 gridBagConstraints37.insets = new Insets(0, 0, 6, 0);
		 gridBagConstraints37.gridy = 14;
		 gridBagConstraints37.ipadx = 50;
		 gridBagConstraints37.ipady = 5;
		 gridBagConstraints37.gridx = 0;
		 final GridBagConstraints gridBagConstraints34 = new GridBagConstraints();
		 gridBagConstraints34.gridx = 1;
		 gridBagConstraints34.gridy = 11;
		 gridBagConstraints34.ipadx = 200;
		 gridBagConstraints34.ipady = 10;
		 gridBagConstraints34.weightx = 1.0;
		 gridBagConstraints34.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints33 = new GridBagConstraints();
		 gridBagConstraints33.gridx = 0;
		 gridBagConstraints33.ipadx = 152;
		 gridBagConstraints33.ipady = 25;
		 gridBagConstraints33.gridy = 11;
		 final GridBagConstraints gridBagConstraints32 = new GridBagConstraints();
		 gridBagConstraints32.gridx = 1;
		 gridBagConstraints32.gridy = 10;
		 gridBagConstraints32.ipadx = 200;
		 gridBagConstraints32.ipady = 10;
		 gridBagConstraints32.weightx = 1.0;
		 gridBagConstraints32.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
		 gridBagConstraints31.gridx = 0;
		 gridBagConstraints31.ipadx = 23;
		 gridBagConstraints31.ipady = 25;
		 gridBagConstraints31.gridy = 10;
		 final GridBagConstraints gridBagConstraints29 = new GridBagConstraints();
		 gridBagConstraints29.gridx = 0;
		 gridBagConstraints29.ipadx = 74;
		 gridBagConstraints29.ipady = 25;
		 gridBagConstraints29.gridy = 9;
		 final GridBagConstraints gridBagConstraints28 = new GridBagConstraints();
		 gridBagConstraints28.insets = new Insets(0, 0, 0, 1);
		 gridBagConstraints28.gridy = 7;
		 gridBagConstraints28.ipadx = 120;
		 gridBagConstraints28.ipady = 17;
		 gridBagConstraints28.gridx = 1;
		 final GridBagConstraints gridBagConstraints27 = new GridBagConstraints();
		 gridBagConstraints27.gridx = 0;
		 gridBagConstraints27.ipadx = 111;
		 gridBagConstraints27.ipady = 25;
		 gridBagConstraints27.gridy = 7;
		 final GridBagConstraints gridBagConstraints26 = new GridBagConstraints();
		 gridBagConstraints26.gridx = 1;
		 gridBagConstraints26.gridy = 6;
		 gridBagConstraints26.ipadx = 200;
		 gridBagConstraints26.ipady = 10;
		 gridBagConstraints26.weightx = 1.0;
		 gridBagConstraints26.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints25 = new GridBagConstraints();
		 gridBagConstraints25.gridx = 0;
		 gridBagConstraints25.ipadx = 135;
		 gridBagConstraints25.ipady = 25;
		 gridBagConstraints25.gridy = 6;
		 final GridBagConstraints gridBagConstraints24 = new GridBagConstraints();
		 gridBagConstraints24.gridx = 1;
		 gridBagConstraints24.gridy = 5;
		 gridBagConstraints24.ipadx = 200;
		 gridBagConstraints24.ipady = 10;
		 gridBagConstraints24.weightx = 1.0;
		 gridBagConstraints24.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints23 = new GridBagConstraints();
		 gridBagConstraints23.gridx = 0;
		 gridBagConstraints23.ipadx = 155;
		 gridBagConstraints23.ipady = 25;
		 gridBagConstraints23.gridy = 5;
		 final GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
		 gridBagConstraints22.gridx = 1;
		 gridBagConstraints22.gridy = 4;
		 gridBagConstraints22.ipadx = 200;
		 gridBagConstraints22.ipady = 10;
		 gridBagConstraints22.weightx = 1.0;
		 gridBagConstraints22.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
		 gridBagConstraints21.gridx = 0;
		 gridBagConstraints21.ipadx = 141;
		 gridBagConstraints21.ipady = 25;
		 gridBagConstraints21.gridy = 4;
		 final GridBagConstraints gridBagConstraints20 = new GridBagConstraints();
		 gridBagConstraints20.gridx = 1;
		 gridBagConstraints20.gridy = 3;
		 gridBagConstraints20.ipadx = 200;
		 gridBagConstraints20.ipady = 10;
		 gridBagConstraints20.weightx = 1.0;
		 gridBagConstraints20.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
		 gridBagConstraints19.gridx = 0;
		 gridBagConstraints19.ipadx = 174;
		 gridBagConstraints19.ipady = 25;
		 gridBagConstraints19.gridy = 3;
		 final GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
		 gridBagConstraints18.gridx = 1;
		 gridBagConstraints18.gridy = 2;
		 gridBagConstraints18.ipadx = 200;
		 gridBagConstraints18.ipady = 10;
		 gridBagConstraints18.weightx = 1.0;
		 gridBagConstraints18.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
		 gridBagConstraints17.gridx = 0;
		 gridBagConstraints17.ipadx = 160;
		 gridBagConstraints17.ipady = 25;
		 gridBagConstraints17.gridy = 2;
		 final GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
		 gridBagConstraints16.insets = new Insets(0, 0, 0, 1);
		 gridBagConstraints16.gridy = 0;
		 gridBagConstraints16.ipadx = 220;
		 gridBagConstraints16.ipady = 25;
		 gridBagConstraints16.gridx = 1;
		 final GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
		 gridBagConstraints15.gridx = 0;
		 gridBagConstraints15.ipadx = 204;
		 gridBagConstraints15.ipady = 25;
		 gridBagConstraints15.gridy = 0;
		 final GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
		 gridBagConstraints13.gridx = 1;
		 gridBagConstraints13.gridy = 1;
		 gridBagConstraints13.ipadx = 200;
		 gridBagConstraints13.ipady = 10;
		 gridBagConstraints13.weightx = 1.0;
		 gridBagConstraints13.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
		 gridBagConstraints14.gridx = 0;
		 gridBagConstraints14.ipadx = 130;
		 gridBagConstraints14.ipady = 25;
		 gridBagConstraints14.gridy = 1;
		 jLabel50 = new JLabel();
		 jLabel50.setText("Limit admin cmds to admin chan:");
		 jLabel43 = new JLabel();
		 jLabel43.setText("KD Location:");
		 jLabel41 = new JLabel();
		 jLabel41.setText("Age Start (YYYY-MM-DD HH:mm:ss):");
		 jLabel39 = new JLabel();
		 jLabel39.setText("Require password to auth:");
		 jLabel38 = new JLabel();
		 jLabel38.setText("Users may register:");
		 jLabel37 = new JLabel();
		 jLabel37.setText("Admin Channel:");
		 jLabel36 = new JLabel();
		 jLabel36.setText("KD Channel:");
		 jLabel10 = new JLabel();
		 jLabel10.setText("Bot password:");
		 jLabel9 = new JLabel();
		 jLabel9.setText("Bot nick:");
		 jLabel8 = new JLabel();
		 jLabel8.setText(" ");
		 jLabel7 = new JLabel();
		 jLabel7.setText("IRC Server:");
		 jLabel666 = new JLabel();
		 jLabel666.setText("Command prefix:");
		 jLabel6 = new JLabel();
		 jLabel6.setText("IRC");
		 baseSettingsContentPane = new JPanel();
		 baseSettingsContentPane.setLayout(new GridBagLayout());
		 baseSettingsContentPane.add(jLabel6, gridBagConstraints15);
		 baseSettingsContentPane.add(jLabel666, gridBagConstraints14);
		 baseSettingsContentPane.add(getCommandPrefix(), gridBagConstraints13);
		 baseSettingsContentPane.add(jLabel8, gridBagConstraints16);
		 baseSettingsContentPane.add(jLabel7, gridBagConstraints17);
		 baseSettingsContentPane.add(getIrcServer(), gridBagConstraints18);
		 baseSettingsContentPane.add(jLabel9, gridBagConstraints19);
		 baseSettingsContentPane.add(getBotNick(), gridBagConstraints20);
		 baseSettingsContentPane.add(jLabel10, gridBagConstraints21);
		 baseSettingsContentPane.add(getBotPassword(), gridBagConstraints22);
		 baseSettingsContentPane.add(jLabel36, gridBagConstraints23);
		 baseSettingsContentPane.add(getKdChannel(), gridBagConstraints24);
		 baseSettingsContentPane.add(jLabel37, gridBagConstraints25);
		 baseSettingsContentPane.add(getAdminChannel(), gridBagConstraints26);
		 baseSettingsContentPane.add(jLabel38, gridBagConstraints27);
		 baseSettingsContentPane.add(getUsersMayRegister(), gridBagConstraints28);
		 baseSettingsContentPane.add(jLabel39, gridBagConstraints29);
		 baseSettingsContentPane.add(jLabel50, gridBagConstraints39);
		 baseSettingsContentPane.add(jLabel41, gridBagConstraints31);
		 baseSettingsContentPane.add(getAgeStart(), gridBagConstraints32);
		 baseSettingsContentPane.add(jLabel43, gridBagConstraints33);
		 baseSettingsContentPane.add(getKdLoc(), gridBagConstraints34);
		 baseSettingsContentPane.add(getSaveBaseSettings(), gridBagConstraints37);
		 baseSettingsContentPane.add(getCancelBaseSettings(), gridBagConstraints38);
		 baseSettingsContentPane.add(jLabel49, gridBagConstraints47);
		 baseSettingsContentPane.add(getAdminLimit(), gridBagConstraints48);
		 baseSettingsContentPane.add(getPasswordAuth(), gridBagConstraints30);
	  }
	  return baseSettingsContentPane;
   }

   /**
    * This method initializes baseSettingsDialog
    * 
    * @return javax.swing.JDialog
    */
   private JDialog getBaseSettingsDialog() {
	  if (baseSettingsDialog == null) {
		 baseSettingsDialog = new JDialog(getJFrame());
		 baseSettingsDialog.setSize(new Dimension(463, 620));
		 baseSettingsDialog.setTitle("Base Settings");
		 baseSettingsDialog.setContentPane(getBaseSettingsContentPane());
		 baseSettingsDialog.setVisible(false);
	  }
	  return baseSettingsDialog;
   }

   private JPanel getBotMainPanel() {
	  if (botMainPanel == null) {
		 botMainPanel = new JPanel();
		 botMainPanel.setLayout(new GridLayout(0, 3));
		 botMainPanel.add(getBaseSettings());
		 botMainPanel.add(getEditServices());
		 botMainPanel.add(getEditUsers());
		 botMainPanel.add(getEditLinks());
		 botMainPanel.add(getEditCommands());
		 botMainPanel.add(getEditSciences());
		 botMainPanel.add(getEditBuildings());
		 botMainPanel.add(getEditRaces());
		 botMainPanel.add(getEditPersonalities());
		 botMainPanel.add(getEditTitles());
		 botMainPanel.add(getEditSMSSettings(), null);
		 botMainPanel.add(getEditHelp());
		 botMainPanel.add(getEditTextInput());
		 botMainPanel.add(getEditWorkers());
		 botMainPanel.add(getEditGoogleSettings());
	  }
	  return botMainPanel;
   }

   /**
    * This method initializes botNick
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getBotNick() {
	  if (botNick == null) {
		 botNick = new JTextField(25);
		 botNick.getDocument().addDocumentListener(new InstallDocumentListener());
	  }
	  return botNick;
   }

   /**
    * This method initializes botPassword
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getBotPassword() {
	  if (botPassword == null) {
		 botPassword = new JTextField(25);
		 botPassword.getDocument().addDocumentListener(new InstallDocumentListener());
	  }
	  return botPassword;
   }

   /**
    * This method initializes cancelBaseSettings
    * 
    * @return javax.swing.JButton
    */
   private JButton getCancelBaseSettings() {
	  if (cancelBaseSettings == null) {
		 cancelBaseSettings = new JButton();
		 cancelBaseSettings.setText("Cancel");
		 cancelBaseSettings.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   baseSettingsDialog.setVisible(false);
			}
		 });
	  }
	  return cancelBaseSettings;
   }

   /**
    * This method initializes cancelInstallDialog
    * 
    * @return javax.swing.JButton
    */
   private JButton getCancelInstallDialog() {
	  if (cancelInstallDialog == null) {
		 cancelInstallDialog = new JButton();
		 cancelInstallDialog.setText("Cancel");
		 cancelInstallDialog.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   installDialog.setVisible(false);
			   dbName.setText("");
			   dbUser.setText("");
			   dbPassword.setText("");
			   superAdmin.setText("");
			}
		 });
	  }
	  return cancelInstallDialog;
   }

   private JTextField getCommandPrefix() {
	  if (commandPrefix == null) {
		 commandPrefix = new JTextField(25);
		 commandPrefix.getDocument().addDocumentListener(new InstallDocumentListener());
	  }
	  return commandPrefix;
   }

   /**
    * This method initializes connectButton
    * 
    * @return javax.swing.JToggleButton
    */
   private JToggleButton getConnectButton() {
	  if (connectButton == null) {
		 connectButton = new JToggleButton();
		 connectButton.setIcon(new ImageIcon(getClass().getResource("/icons/connect.png")));
		 connectButton.setToolTipText("Connect/Disconnect");
		 connectButton.setEnabled(false);
		 connectButton.setSize(new Dimension(48, 48));
		 connectButton.setPressedIcon(new ImageIcon(getClass().getResource("/icons/disconnect.png")));
		 connectButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   if (connectButton.isSelected() && Main.getBotSettings().hasRequiredFields()) {
				  try {
					 Bot.getInstance().connectToIRC();
				  } catch (final Exception e1) {
					 Logger.getLogger("Error").error("", e1);
				  }
			   } else if (connectButton.isSelected()) {
				  connectButton.setSelected(true);
			   } else {
				  Bot.getInstance().disconnectFromIRC();
			   }
			}
		 });
	  }
	  return connectButton;
   }

   /**
    * This method initializes createDB
    * 
    * @return javax.swing.JButton
    */
   private JButton getCreateDB() {
	  if (createDB == null) {
		 createDB = new JButton();
		 createDB.setText("Create Database");
		 createDB.setEnabled(false);
		 createDB.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   if (DBConnectionFactory.getNewInstallHandler(dbHost.getText(), dbName.getText(), dbUser.getText(),
					 dbPassword.getText()).runInstall()) {
				  SettingsHandler ss = new SettingsHandler();
				  ss.saveDBSettings(dbHost.getText(), dbName.getText(), dbUser.getText(), dbPassword.getText());
				  ss.loadBotSettings(Main.getBotSettings());
				  if (DBConnectionFactory.getConnectionHandler().connectionIsValid()) {
					 superAdmin.setEnabled(true);
					 addSuperAdmin.setEnabled(true);
					 createDB.setEnabled(false);
				  }
			   } else {
				  showErrorMessage("Failed to create the database", installContentPane);
			   }
			}
		 });
	  }
	  return createDB;
   }

   /**
    * This method initializes dbHost
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getDbHost() {
	  if (dbHost == null) {
		 dbHost = new JTextField();
		 dbHost.setColumns(25);
		 dbHost.setText("127.0.0.1");
		 dbHost.setPreferredSize(new Dimension(279, 20));
	  }
	  return dbHost;
   }

   /**
    * This method initializes dbName
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getDbName() {
	  if (dbName == null) {
		 dbName = new JTextField(25);
		 dbName.addKeyListener(new java.awt.event.KeyAdapter() {

			@Override
			public void keyTyped(final java.awt.event.KeyEvent e) {
			   createDB.setEnabled(false);
			}
		 });
		 dbName.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   if (!"".equals(dbName.getText())) {
				  createDB.setEnabled(true);
			   } else {
				  createDB.setEnabled(false);
			   }
			}
		 });
	  }
	  return dbName;
   }

   /**
    * This method initializes dbPassword
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getDbPassword() {
	  if (dbPassword == null) {
		 dbPassword = new JTextField(25);
		 dbPassword.addKeyListener(new java.awt.event.KeyAdapter() {

			@Override
			public void keyTyped(final java.awt.event.KeyEvent e) {
			   createDB.setEnabled(false);
			}
		 });
	  }
	  return dbPassword;
   }

   /**
    * This method initializes dbUser
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getDbUser() {
	  if (dbUser == null) {
		 dbUser = new JTextField(25);
		 dbUser.addKeyListener(new java.awt.event.KeyAdapter() {

			@Override
			public void keyTyped(final java.awt.event.KeyEvent e) {
			   createDB.setEnabled(false);
			}
		 });
	  }
	  return dbUser;
   }

   private JButton getEditBuildings() {
	  if (editBuildings == null) {
		 editBuildings = new JButton();
		 editBuildings.setText("Edit Buildings");
		 editBuildings.setEnabled(true);
		 editBuildings.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT name FROM building");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new BuildingsPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editBuildings;
   }

   private JButton getEditCommands() {
	  if (editCommands == null) {
		 editCommands = new JButton();
		 editCommands.setText("Edit Commands");
		 editCommands.setEnabled(true);
		 editCommands.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT name FROM commands");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new CommandsPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editCommands;
   }

   private JButton getEditGoogleSettings() {
	  if (google == null) {
		 google = new JButton();
		 google.setText("Edit Google Settings");
		 google.setEnabled(true);
		 google.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final ArrayList<String> itemList = new ArrayList<String>();
			   itemList.add("API Key");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new GooglePanel(dp), false));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return google;
   }

   private JButton getEditHelp() {
	  if (editHelp == null) {
		 editHelp = new JButton();
		 editHelp.setText("Edit Help");
		 editHelp.setEnabled(true);
		 editHelp.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT topic FROM help");

			   DialogPanel dp = new DialogPanel();
			   dp.init(itemList, new HelpPanel(dp), true);
			   dp.setSaveButtonEnabled(true);
			   settingsDialog.add(dp);
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editHelp;
   }

   private JButton getEditLinks() {
	  if (editLinks == null) {
		 editLinks = new JButton();
		 editLinks.setText("Edit Links");
		 editLinks.setEnabled(true);
		 editLinks.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT name FROM link");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new LinksPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editLinks;
   }

   private JButton getEditPersonalities() {
	  if (editPersonalities == null) {
		 editPersonalities = new JButton();
		 editPersonalities.setText("Edit Personalities");
		 editPersonalities.setEnabled(true);
		 editPersonalities.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT name FROM personality");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new PersonalitiesPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editPersonalities;
   }

   private JButton getEditRaces() {
	  if (editRaces == null) {
		 editRaces = new JButton();
		 editRaces.setText("Edit Races");
		 editRaces.setEnabled(true);
		 editRaces.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT name FROM race");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new RacesPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editRaces;
   }

   private JButton getEditSciences() {
	  if (editSciences == null) {
		 editSciences = new JButton();
		 editSciences.setText("Edit Sciences");
		 editSciences.setEnabled(true);
		 editSciences.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT name FROM science");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new SciencesPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editSciences;
   }

   private JButton getEditServices() {
	  if (services == null) {
		 services = new JButton();
		 services.setText("Edit Services Settings");
		 services.setEnabled(true);
		 services.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final ArrayList<String> itemList = new ArrayList<String>();
			   itemList.add("intel");
			   itemList.add("rpcservice");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new ServicesPanel(dp), false));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return services;
   }

   /**
    * This method initializes editSMSSettings
    * 
    * @return javax.swing.JButton
    */
   private JButton getEditSMSSettings() {
	  if (editSMSSettings == null) {
		 editSMSSettings = new JButton();
		 editSMSSettings.setText("Edit SMS Settings");
		 editSMSSettings.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final ArrayList<String> itemList = new ArrayList<String>();
			   itemList.add("sms");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new SMSPanel(dp), false));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editSMSSettings;
   }

   private JButton getEditTextInput() {
	  if (editTextInput == null) {
		 editTextInput = new JButton();
		 editTextInput.setText("Edit Text Input");
		 editTextInput.setEnabled(true);
		 editTextInput.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT name FROM textinput");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new TextInputPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editTextInput;
   }

   private JButton getEditTitles() {
	  if (editTitles == null) {
		 editTitles = new JButton();
		 editTitles.setText("Edit Titles");
		 editTitles.setEnabled(true);
		 editTitles.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT name FROM title");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new TitlesPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editTitles;
   }

   private JButton getEditUsers() {
	  if (editUsers == null) {
		 editUsers = new JButton();
		 editUsers.setText("Edit Users");
		 editUsers.setEnabled(true);
		 editUsers.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT mainnick FROM kdmember");
			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new UsersPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editUsers;
   }

   private JButton getEditWorkers() {
	  if (editWorkers == null) {
		 editWorkers = new JButton();
		 editWorkers.setText("Edit Workers");
		 editWorkers.setEnabled(true);
		 editWorkers.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   settingsDialog.getContentPane().removeAll();

			   final List<String> itemList = getItemList("SELECT nick FROM worker");

			   DialogPanel dp = new DialogPanel();
			   settingsDialog.add(dp.init(itemList, new WorkersPanel(dp), true));
			   settingsDialog.setVisible(true);
			   settingsDialog.validate();
			}
		 });
	  }
	  return editWorkers;
   }

   /**
    * This method initializes exitButton
    * 
    * @return javax.swing.JButton
    */
   private JButton getExitButton() {
	  if (exitButton == null) {
		 exitButton = new JButton();
		 exitButton.setSize(new Dimension(48, 48));
		 exitButton.setIcon(new ImageIcon(getClass().getResource("/icons/exit.png")));
		 exitButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   try {
				  Bot.getInstance().disconnectFromDB();
			   } catch (final Exception e1) {
				  Logger.getLogger("Error").error("", e1);
			   }
			   Bot.getInstance().disconnect();
			   System.exit(0);
			}
		 });
	  }
	  return exitButton;
   }

   /**
    * This method initializes goToBaseSettingsDialog
    * 
    * @return javax.swing.JButton
    */
   private JButton getGoToBaseSettingsDialog() {
	  if (goToBaseSettingsDialog == null) {
		 goToBaseSettingsDialog = new JButton();
		 goToBaseSettingsDialog.setText("Next");
		 goToBaseSettingsDialog.setEnabled(false);
		 goToBaseSettingsDialog.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   installDialog.setVisible(false);
			   if (DBConnectionFactory.getConnectionHandler().connectionIsValid()) {
				  dbHost.setText("");
				  dbName.setText("");
				  dbUser.setText("");
				  dbPassword.setText("");
				  superAdmin.setText("");
				  goToBaseSettingsDialog.setEnabled(false);
				  setBaseSettingsToDialog();
				  baseSettingsDialog.setVisible(true);
				  newAgeButton.setEnabled(true);
			   }
			}
		 });
	  }
	  return goToBaseSettingsDialog;
   }

   /**
    * This method initializes goToBotButton
    * 
    * @return javax.swing.JButton
    */
   private JButton getGoToBotButton() {
	  if (goToBotButton == null) {
		 goToBotButton = new JButton();
		 goToBotButton.setText("Go to Bot");
		 goToBotButton.setEnabled(Main.getBotSettings().hasRequiredFields()
			   && DBConnectionFactory.getConnectionHandler().connectionIsValid());
		 goToBotButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   jContentPane.remove(basePanel);
			   jJToolBarBar1.setVisible(true);
			   jContentPane.add(getBotMainPanel());
			   botMainPanel.setVisible(true);
			   jContentPane.repaint();
			   if (Main.getBotSettings().hasRequiredFields() && DBConnectionFactory.getConnectionHandler().connectionIsValid()) {
				  connectButton.setEnabled(true);
			   } else {
				  connectButton.setEnabled(false);
			   }
			}
		 });
	  }
	  return goToBotButton;
   }

   /**
    * This method initializes installButton
    * 
    * @return javax.swing.JButton
    */
   private JButton getInstallButton() {
	  if (installButton == null) {
		 installButton = new JButton();
		 installButton.setText("Install Bot");
		 installButton.setMnemonic(KeyEvent.VK_UNDEFINED);
		 installButton.setSize(75, 30);
		 installButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   installDialog.setVisible(true);
			}
		 });
	  }
	  return installButton;
   }

   /**
    * This method initializes installContentPane
    * 
    * @return javax.swing.JPanel
    */
   private JPanel getInstallContentPane() {
	  if (installContentPane == null) {
		 GridBagConstraints gridBagConstraints36 = new GridBagConstraints();
		 gridBagConstraints36.fill = GridBagConstraints.NONE;
		 gridBagConstraints36.gridy = 0;
		 gridBagConstraints36.weightx = 1.0;
		 gridBagConstraints36.insets = new Insets(0, 0, 0, 1);
		 gridBagConstraints36.ipadx = 200;
		 gridBagConstraints36.ipady = 10;
		 gridBagConstraints36.gridx = 1;
		 GridBagConstraints gridBagConstraints35 = new GridBagConstraints();
		 gridBagConstraints35.gridx = 0;
		 gridBagConstraints35.ipadx = 23;
		 gridBagConstraints35.ipady = 15;
		 gridBagConstraints35.gridy = 0;
		 jLabel11 = new JLabel();
		 jLabel11.setText("Database Host:");
		 jLabel11.setPreferredSize(new Dimension(92, 16));
		 final GridBagConstraints gridBagConstraints46 = new GridBagConstraints();
		 gridBagConstraints46.gridx = 1;
		 gridBagConstraints46.gridy = 10;
		 jLabel48 = new JLabel();
		 jLabel48.setText(" ");
		 final GridBagConstraints gridBagConstraints45 = new GridBagConstraints();
		 gridBagConstraints45.gridx = 0;
		 gridBagConstraints45.gridy = 11;
		 final GridBagConstraints gridBagConstraints44 = new GridBagConstraints();
		 gridBagConstraints44.gridx = 1;
		 gridBagConstraints44.gridy = 11;
		 final GridBagConstraints gridBagConstraints43 = new GridBagConstraints();
		 gridBagConstraints43.gridx = 1;
		 gridBagConstraints43.gridy = 8;
		 jLabel47 = new JLabel();
		 jLabel47.setText(" ");
		 final GridBagConstraints gridBagConstraints42 = new GridBagConstraints();
		 gridBagConstraints42.gridx = 1;
		 gridBagConstraints42.gridy = 9;
		 gridBagConstraints42.ipadx = 50;
		 gridBagConstraints42.ipady = 10;
		 final GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
		 gridBagConstraints41.gridx = 1;
		 gridBagConstraints41.gridy = 6;
		 jLabel46 = new JLabel();
		 jLabel46.setText(" ");
		 final GridBagConstraints gridBagConstraints40 = new GridBagConstraints();
		 gridBagConstraints40.fill = GridBagConstraints.VERTICAL;
		 gridBagConstraints40.gridy = 7;
		 gridBagConstraints40.weightx = 1.0;
		 gridBagConstraints40.gridx = 1;
		 gridBagConstraints40.ipadx = 200;
		 gridBagConstraints40.ipady = 10;
		 final GridBagConstraints gridBagConstraints39 = new GridBagConstraints();
		 gridBagConstraints39.gridx = 0;
		 gridBagConstraints39.gridy = 7;
		 gridBagConstraints39.ipadx = 35;
		 gridBagConstraints39.ipady = 15;
		 jLabel45 = new JLabel();
		 jLabel45.setText("Super Admin:");
		 final GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
		 gridBagConstraints14.gridx = 1;
		 gridBagConstraints14.gridy = 4;
		 credentialsTestLabel = new JLabel();
		 credentialsTestLabel.setText(" ");
		 final GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
		 gridBagConstraints13.insets = new Insets(0, 0, 0, 1);
		 gridBagConstraints13.gridy = 5;
		 gridBagConstraints13.ipadx = 20;
		 gridBagConstraints13.ipady = 10;
		 gridBagConstraints13.gridx = 1;
		 final GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
		 gridBagConstraints12.gridx = 0;
		 gridBagConstraints12.ipadx = 20;
		 gridBagConstraints12.ipady = 10;
		 gridBagConstraints12.gridy = 5;
		 final GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		 gridBagConstraints11.gridx = 1;
		 gridBagConstraints11.gridy = 3;
		 gridBagConstraints11.ipadx = 200;
		 gridBagConstraints11.ipady = 10;
		 gridBagConstraints11.weightx = 1.0;
		 gridBagConstraints11.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
		 gridBagConstraints10.gridx = 0;
		 gridBagConstraints10.ipadx = 50;
		 gridBagConstraints10.ipady = 15;
		 gridBagConstraints10.gridy = 3;
		 final GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
		 gridBagConstraints9.gridx = 1;
		 gridBagConstraints9.gridy = 2;
		 gridBagConstraints9.ipadx = 200;
		 gridBagConstraints9.ipady = 10;
		 gridBagConstraints9.weightx = 1.0;
		 gridBagConstraints9.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
		 gridBagConstraints8.gridx = 0;
		 gridBagConstraints8.ipadx = 80;
		 gridBagConstraints8.ipady = 15;
		 gridBagConstraints8.gridy = 2;
		 final GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		 gridBagConstraints7.gridx = 1;
		 gridBagConstraints7.gridy = 1;
		 gridBagConstraints7.ipadx = 200;
		 gridBagConstraints7.ipady = 10;
		 gridBagConstraints7.weightx = 1.0;
		 gridBagConstraints7.insets = new Insets(0, 0, 0, 1);
		 final GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		 gridBagConstraints6.gridx = 0;
		 gridBagConstraints6.ipadx = 20;
		 gridBagConstraints6.ipady = 15;
		 gridBagConstraints6.gridy = 1;
		 jLabel5 = new JLabel();
		 jLabel5.setText("Password:");
		 jLabel4 = new JLabel();
		 jLabel4.setText("User:");
		 jLabel3 = new JLabel();
		 jLabel3.setText("Database name:");
		 installContentPane = new JPanel();
		 installContentPane.setLayout(new GridBagLayout());
		 installContentPane.add(jLabel3, gridBagConstraints6);
		 installContentPane.add(getDbName(), gridBagConstraints7);
		 installContentPane.add(jLabel4, gridBagConstraints8);
		 installContentPane.add(getDbUser(), gridBagConstraints9);
		 installContentPane.add(jLabel5, gridBagConstraints10);
		 installContentPane.add(getDbPassword(), gridBagConstraints11);
		 installContentPane.add(getTestDBCredentials(), gridBagConstraints12);
		 installContentPane.add(getCreateDB(), gridBagConstraints13);
		 installContentPane.add(credentialsTestLabel, gridBagConstraints14);
		 installContentPane.add(jLabel45, gridBagConstraints39);
		 installContentPane.add(getSuperAdmin(), gridBagConstraints40);
		 installContentPane.add(jLabel46, gridBagConstraints41);
		 installContentPane.add(getAddSuperAdmin(), gridBagConstraints42);
		 installContentPane.add(jLabel47, gridBagConstraints43);
		 installContentPane.add(getGoToBaseSettingsDialog(), gridBagConstraints44);
		 installContentPane.add(getCancelInstallDialog(), gridBagConstraints45);
		 installContentPane.add(jLabel48, gridBagConstraints46);
		 installContentPane.add(jLabel11, gridBagConstraints35);
		 installContentPane.add(getDbHost(), gridBagConstraints36);
	  }
	  return installContentPane;
   }

   /**
    * This method initializes installDialog
    * 
    * @return javax.swing.JDialog
    */
   private JDialog getInstallDialog() {
	  if (installDialog == null) {
		 installDialog = new JDialog(getJFrame());
		 installDialog.setSize(new Dimension(471, 466));
		 installDialog.setTitle("Setup database");
		 installDialog.setContentPane(getInstallContentPane());
		 installDialog.setVisible(false);
	  }
	  return installDialog;
   }

   /**
    * This method initializes ircServer
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getIrcServer() {
	  if (ircServer == null) {
		 ircServer = new JTextField(25);
		 ircServer.getDocument().addDocumentListener(new InstallDocumentListener());
	  }
	  return ircServer;
   }

   /**
    * Returns a list of items based on querying the database with the query parameter
    * 
    * @param query
    * @return
    */
   private List<String> getItemList(final String query) {
	  Statement stmt = null;
	  ResultSet rs = null;
	  final ArrayList<String> out = new ArrayList<String>();
	  try {
		 stmt = DBConnectionFactory.getConnectionHandler().getConnection().createStatement();
		 rs = stmt.executeQuery(query);

		 while (rs.next()) {
			out.add(rs.getString(1));
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			stmt.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }

   /**
    * This method initializes jContentPane
    * 
    * @return javax.swing.JPanel
    */
   private JPanel getJContentPane() {
	  if (jContentPane == null) {
		 jContentPane = new JPanel();
		 jContentPane.setLayout(new BorderLayout());
		 jContentPane.add(getJJToolBarBar1(), BorderLayout.PAGE_START);
		 jContentPane.add(getBasePanel(), BorderLayout.CENTER);
	  }
	  return jContentPane;
   }

   /**
    * This method initializes jFrame
    * 
    * @return javax.swing.JFrame
    */
   private JFrame getJFrame() {
	  if (jFrame == null) {
		 jFrame = new JFrame();
		 jFrame.setSize(new Dimension(501, 493));
		 jFrame.setTitle("LucidBot");
		 jFrame.setContentPane(getJContentPane());
		 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 jFrame.setVisible(true);
	  }
	  return jFrame;
   }

   /**
    * This method initializes jJToolBarBar1
    * 
    * @return javax.swing.JToolBar
    */
   private JToolBar getJJToolBarBar1() {
	  if (jJToolBarBar1 == null) {
		 jJToolBarBar1 = new JToolBar();
		 jJToolBarBar1.setLayout(new BoxLayout(getJJToolBarBar1(), BoxLayout.X_AXIS));
		 jJToolBarBar1.setFloatable(false);
		 jJToolBarBar1.setPreferredSize(new Dimension(100, 38));
		 jJToolBarBar1.add(getConnectButton());
		 jJToolBarBar1.add(getToTrayButton());
		 jJToolBarBar1.add(getExitButton());
		 jJToolBarBar1.setVisible(false);
	  }
	  return jJToolBarBar1;
   }

   /**
    * This method initializes kdChannel
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getKdChannel() {
	  if (kdChannel == null) {
		 kdChannel = new JTextField(25);
		 kdChannel.getDocument().addDocumentListener(new InstallDocumentListener());
	  }
	  return kdChannel;
   }

   /**
    * This method initializes kdLoc
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getKdLoc() {
	  if (kdLoc == null) {
		 kdLoc = new JTextField(25);
		 kdLoc.getDocument().addDocumentListener(new InstallDocumentListener());
	  }
	  return kdLoc;
   }

   /**
    * This method initializes newAgeButton
    * 
    * @return javax.swing.JButton
    */
   private JButton getNewAgeButton() {
	  if (newAgeButton == null) {
		 newAgeButton = new JButton();
		 newAgeButton.setText("New Age");
		 newAgeButton.setEnabled(Main.getBotSettings().hasRequiredFields()
			   && DBConnectionFactory.getConnectionHandler().connectionIsValid());
		 newAgeButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   DBConnectionFactory.getConnectionHandler().clearForNewAge();
			}
		 });
	  }
	  return newAgeButton;
   }

   /**
    * This method initializes passwordAuth
    * 
    * @return javax.swing.JCheckBox
    */
   private JCheckBox getPasswordAuth() {
	  if (passwordAuth == null) {
		 passwordAuth = new JCheckBox();
		 passwordAuth.setActionCommand("Select for yes");
		 passwordAuth.setText("Select for yes");
		 passwordAuth.setPreferredSize(new Dimension(103, 24));
		 passwordAuth.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(final javax.swing.event.ChangeEvent e) {
			   if (!ircServer.getText().equals("") && !botNick.getText().equals("") && kdChannel.getText().matches("[#]{1}.*")
					 && ageStart.getText().matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")
					 && kdLoc.getText().matches("[0-9]{1,2}:[0-9]{1,2}") && !botPassword.getText().equals("")) {
				  saveBaseSettings.setEnabled(true);
			   } else {
				  saveBaseSettings.setEnabled(false);
			   }
			}
		 });
	  }
	  return passwordAuth;
   }

   /**
    * This method initializes saveBaseSettings
    * 
    * @return javax.swing.JButton
    */
   private JButton getSaveBaseSettings() {
	  if (saveBaseSettings == null) {
		 saveBaseSettings = new JButton();
		 saveBaseSettings.setText("OK");
		 saveBaseSettings.setEnabled(false);
		 saveBaseSettings.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   if (commandPrefix.getText().length() == 1 && !ircServer.getText().equals("") && !botNick.getText().equals("")
					 && kdChannel.getText().matches("[#]{1}.*")
					 && ageStart.getText().matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")
					 && kdLoc.getText().matches("[0-9]{1,2}:[0-9]{1,2}") && !botPassword.getText().equals("")) {
				  Main.getBotSettings().setIrcServer(ircServer.getText());
				  Main.getBotSettings().setBotNick(botNick.getText());
				  Main.getBotSettings().setBotPassword(botPassword.getText());
				  Main.getBotSettings().setMainChannel(kdChannel.getText());
				  Main.getBotSettings().setAdminChannel(adminChannel.getText());
				  Main.getBotSettings().setUsersMayRegister(usersMayRegister.isSelected());
				  Main.getBotSettings().setLimitAdminCommands(adminLimit.isSelected());
				  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				  sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
				  try {
					 Main.getBotSettings().setAgeStart(sdf.parse(ageStart.getText()).getTime());
				  } catch (ParseException e1) {
					 Logger.getLogger("Error").error("", e1);
				  }
				  Main.getBotSettings().setKdLoc(kdLoc.getText());
				  Main.getBotSettings().setPasswordAuth(passwordAuth.isSelected());
				  Main.getBotSettings().setCommandPrefix(commandPrefix.getText());
			   }

			   if (new SettingsHandler().saveBaseSettings()) {
				  new SettingsHandler().loadBotSettings(Main.getBotSettings());
				  baseSettingsDialog.setVisible(false);
				  newAgeButton.setEnabled(true);
				  goToBotButton.setEnabled(true);
				  if (Main.getBotSettings().hasRequiredFields() && DBConnectionFactory.getConnectionHandler().connectionIsValid()) {
					 connectButton.setEnabled(true);
				  } else {
					 connectButton.setEnabled(false);
				  }
			   } else {
				  showErrorMessage("Failed to save base settings", getBaseSettingsContentPane());
			   }
			}
		 });
	  }
	  return saveBaseSettings;
   }

   private JDialog getSettingsDialog() {
	  if (settingsDialog == null) {
		 settingsDialog = new JDialog(getJFrame());
		 settingsDialog.setSize(new Dimension(500, 590));
		 settingsDialog.setTitle("Settings");
		 final JPanel content = new JPanel();
		 content.setLayout(new FlowLayout());
		 settingsDialog.setContentPane(content);
		 settingsDialog.setVisible(false);
		 settingsDialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
	  }
	  return settingsDialog;
   }

   /**
    * This method initializes superAdmin
    * 
    * @return javax.swing.JTextField
    */
   private JTextField getSuperAdmin() {
	  if (superAdmin == null) {
		 superAdmin = new JTextField(25);
		 superAdmin.setEnabled(false);
		 superAdmin.addKeyListener(new java.awt.event.KeyAdapter() {

			@Override
			public void keyTyped(final java.awt.event.KeyEvent e) {
			   addSuperAdmin.setEnabled(true);
			}
		 });
	  }
	  return superAdmin;
   }

   /**
    * This method initializes TestDBCredentials
    * 
    * @return javax.swing.JButton
    */
   private JButton getTestDBCredentials() {
	  if (testDBCredentials == null) {
		 testDBCredentials = new JButton();
		 testDBCredentials.setText("Test credentials");
		 testDBCredentials.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   ConnectionHandler con = DBConnectionFactory.getNewConnectionHandler(dbHost.getText(), dbName.getText(),
					 dbUser.getText(), dbPassword.getText());
			   if (con.getConnection() == null) {
				  credentialsTestLabel.setText("Credentials failed");
			   } else if ("".equals(dbName.getText())) {
				  credentialsTestLabel.setText("Credentials verified");
			   } else {
				  credentialsTestLabel.setText("Credentials verified");
				  if (con.connectionIsValid()) {
					 if (con.canCreateDB()) {
						createDB.setEnabled(true);
					 }
					 superAdmin.setEnabled(true);
					 addSuperAdmin.setEnabled(true);
					 goToBaseSettingsDialog.setEnabled(true);
					 if (!new SettingsHandler().saveDBSettings(dbHost.getText(), dbName.getText(), dbUser.getText(),
						   dbPassword.getText())) {
						showErrorMessage("Failed to save settings", basePanel);
					 }
					 new SettingsHandler().loadBotSettings(Main.getBotSettings());
				  } else if (!con.connectionIsValid() && !"".equals(dbName.getText()) && con.canCreateDB()) {
					 createDB.setEnabled(true);
				  } else {
					 credentialsTestLabel.setText("Can't create db");
				  }
			   }
			}
		 });
	  }
	  return testDBCredentials;
   }

   /**
    * This method initializes connectButton
    * 
    * @return javax.swing.JToggleButton
    */
   private JButton getToTrayButton() {
	  if (toTrayButton == null) {
		 toTrayButton = new JButton();
		 toTrayButton.setSize(new Dimension(105, 48));
		 toTrayButton.setIcon(new ImageIcon(getClass().getResource("/icons/tray.png")));
		 toTrayButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent e) {
			   jFrame.setVisible(false);
			}
		 });
	  }
	  return toTrayButton;
   }

   /**
    * This method initializes usersMayRegister
    * 
    * @return javax.swing.JCheckBox
    */
   private JCheckBox getUsersMayRegister() {
	  if (usersMayRegister == null) {
		 usersMayRegister = new JCheckBox();
		 usersMayRegister.setText("Select for yes");
		 usersMayRegister.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(final javax.swing.event.ChangeEvent e) {
			   if (!ircServer.getText().equals("") && !botNick.getText().equals("") && kdChannel.getText().matches("[#]{1}.*")
					 && ageStart.getText().matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")
					 && kdLoc.getText().matches("[0-9]{1,2}:[0-9]{1,2}") && !botPassword.getText().equals("")) {
				  saveBaseSettings.setEnabled(true);
			   } else {
				  saveBaseSettings.setEnabled(false);
			   }
			}
		 });
	  }
	  return usersMayRegister;
   }

   /**
    * Inserts the saved settings into the settings dialog
    */
   private void setBaseSettingsToDialog() {
	  commandPrefix.setText(Main.getBotSettings().getCommandPrefix());
	  ircServer.setText(Main.getBotSettings().getIrcServer());
	  botNick.setText(Main.getBotSettings().getBotNick());
	  botPassword.setText(Main.getBotSettings().getBotPassword());
	  kdChannel.setText(Main.getBotSettings().getMainChannel()
			+ ("".equals(Main.getBotSettings().getMainChannelPw()) ? "" : " " + Main.getBotSettings().getMainChannelPw()));
	  adminChannel.setText(Main.getBotSettings().getAdminChannel()
			+ ("".equals(Main.getBotSettings().getAdminChannelPw()) ? "" : " " + Main.getBotSettings().getAdminChannelPw()));
	  if (Main.getBotSettings().usersMayRegister()) {
		 usersMayRegister.setSelected(true);
	  } else {
		 usersMayRegister.setSelected(false);
	  }
	  if (Main.getBotSettings().limitAdminCommands()) {
		 adminLimit.setSelected(true);
	  } else {
		 adminLimit.setSelected(false);
	  }
	  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
	  ageStart.setText(sdf.format(new Date(Main.getBotSettings().getAgeStart())));
	  kdLoc.setText(Main.getBotSettings().getKdLoc());
	  passwordAuth.setSelected(Main.getBotSettings().requirePasswordAuth());
   }

   /**
    * Displays an error message in a popup dialog
    * 
    * @param message
    * @param container
    */
   public final void showErrorMessage(final String message, final JPanel container) {
	  JOptionPane.showMessageDialog(container, message, "Error", JOptionPane.ERROR_MESSAGE);
   }
}
